﻿namespace AnyOffice.DAL
{
    using AnyOffice.Model;
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class OfficeProductDao
    {
        public bool Delete(SqlConnection conn, int productId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("DELETE FROM [dbo].[Office_Transhistory] ");
            builder.Append("WHERE ");
            builder.Append("\t[ProductId] = @ProductId ");
            builder.Append("DELETE FROM [dbo].[Office_Product] ");
            builder.Append("WHERE ");
            builder.Append("\t[ProductId] = @ProductId ");
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@ProductId", SqlDbType.Int) };
            commandParameters[0].Value = productId;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, builder.ToString(), commandParameters);
            return true;
        }

        public int Insert(SqlConnection conn, OfficeProductData data)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("INSERT INTO [dbo].[Office_Product] ");
            builder.Append("( ");
            builder.Append("\t[TypeId], ");
            builder.Append("\t[Name], ");
            builder.Append("\t[Description], ");
            builder.Append("\t[Unit], ");
            builder.Append("\t[Price], ");
            builder.Append("\t[Supplier], ");
            builder.Append("\t[LowStock], ");
            builder.Append("\t[Stock] ");
            builder.Append(") ");
            builder.Append("VALUES ");
            builder.Append("( ");
            builder.Append("\t@TypeId, ");
            builder.Append("\t@Name, ");
            builder.Append("\t@Description, ");
            builder.Append("\t@Unit, ");
            builder.Append("\t@Price, ");
            builder.Append("\t@Supplier, ");
            builder.Append("\t@LowStock, ");
            builder.Append("\t@Stock ");
            builder.Append(") ");
            builder.Append("SELECT SCOPE_IDENTITY() AS RetValue ");
            SqlParameter[] commandParameters = new SqlParameter[8];
            commandParameters[0] = new SqlParameter("@TypeId", SqlDbType.Int);
            commandParameters[0].Value = data.TypeId;
            commandParameters[1] = new SqlParameter("@Name", SqlDbType.NVarChar, 200);
            commandParameters[1].Value = data.Name;
            commandParameters[2] = new SqlParameter("@Description", SqlDbType.NText);
            if (data.DescriptionIsNull)
            {
                commandParameters[2].Value = DBNull.Value;
            }
            else
            {
                commandParameters[2].Value = data.Description;
            }
            commandParameters[3] = new SqlParameter("@Unit", SqlDbType.NVarChar, 20);
            commandParameters[3].Value = data.Unit;
            commandParameters[4] = new SqlParameter("@Price", SqlDbType.Decimal);
            commandParameters[4].Precision = 10;
            commandParameters[4].Scale = 2;
            if (data.PriceIsNull)
            {
                commandParameters[4].Value = DBNull.Value;
            }
            else
            {
                commandParameters[4].Value = data.Price;
            }
            commandParameters[5] = new SqlParameter("@Supplier", SqlDbType.NVarChar, 50);
            if (data.SupplierIsNull)
            {
                commandParameters[5].Value = DBNull.Value;
            }
            else
            {
                commandParameters[5].Value = data.Supplier;
            }
            commandParameters[6] = new SqlParameter("@LowStock", SqlDbType.Int);
            commandParameters[6].Value = data.LowStock;
            commandParameters[7] = new SqlParameter("@Stock", SqlDbType.Int);
            commandParameters[7].Value = data.Stock;
            object obj2 = SqlHelper.ExecuteScalar(conn, CommandType.Text, builder.ToString(), commandParameters);
            if (obj2 != null)
            {
                return Convert.ToInt32(obj2);
            }
            return -1;
        }

        public DataTable SelectAll(SqlConnection conn)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT ");
            builder.Append("\t[ProductId], ");
            builder.Append("\t[TypeId], ");
            builder.Append("\t[Name], ");
            builder.Append("\t[Description], ");
            builder.Append("\t[Unit], ");
            builder.Append("\t[Price], ");
            builder.Append("\t[Supplier], ");
            builder.Append("\t[LowStock], ");
            builder.Append("\t[Stock] ");
            builder.Append("FROM ");
            builder.Append("\t[dbo].[Office_Product] ");
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, builder.ToString());
            set.Tables[0].TableName = "Office_Product";
            return set.Tables[0];
        }

        public DataTable SelectByProductId(SqlConnection conn, int productId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT ");
            builder.Append("\t[ProductId], ");
            builder.Append("\t[TypeId], ");
            builder.Append("\t[Name], ");
            builder.Append("\t[Description], ");
            builder.Append("\t[Unit], ");
            builder.Append("\t[Price], ");
            builder.Append("\t[Supplier], ");
            builder.Append("\t[LowStock], ");
            builder.Append("\t[Stock] ");
            builder.Append("FROM ");
            builder.Append("\t[dbo].[Office_Product] ");
            builder.Append("WHERE ");
            builder.Append("\t[ProductId] = @ProductId ");
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@ProductId", SqlDbType.Int) };
            commandParameters[0].Value = productId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, builder.ToString(), commandParameters);
            set.Tables[0].TableName = "Office_Product";
            return set.Tables[0];
        }

        public DataTable SelectPaged(SqlConnection conn, int pagesize, int pageindex, bool docount)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("IF(@docount=1) ");
            builder.Append("\tSELECT COUNT([ProductId]) FROM [dbo].[Office_Product] ");
            builder.Append("ELSE ");
            builder.Append("\tBEGIN ");
            builder.Append("\t\tDECLARE @IndexTable TABLE(ID int identity(1,1),PK int) ");
            builder.Append("\t\tDECLARE @PageLowerBound int ");
            builder.Append("\t\tDECLARE @PageUpperBound int ");
            builder.Append("\t\tSET @PageLowerBound=(@pageindex-1)*@pagesize ");
            builder.Append("\t\tSET @PageUpperBound=@PageLowerBound+@pagesize ");
            builder.Append("\t\tSET rowcount @PageUpperBound ");
            builder.Append("\t\t ");
            builder.Append("\t\tINSERT INTO @IndexTable(PK) SELECT [ProductId] FROM [dbo].[Office_Product] ORDER BY [ProductId] DESC ");
            builder.Append("\t\t ");
            builder.Append("\t\tSELECT ");
            builder.Append("\t\t\t[Office_Product].*, ");
            builder.Append("\t\t\t[Office_Type].[TypeName], ");
            builder.Append("\t\t\t@pageindex AS pageindex ");
            builder.Append("\t\tFROM ");
            builder.Append("\t\t\t[dbo].[Office_Product], ");
            builder.Append("\t\t\t[dbo].[Office_Type], ");
            builder.Append("\t\t\t@IndexTable t\t\t\t ");
            builder.Append("\t\tWHERE ");
            builder.Append("\t\t\t[Office_Product].[TypeId] = [Office_Type].[TypeId] ");
            builder.Append("\t\t\tAND [Office_Product].[ProductId] = t.PK ");
            builder.Append("\t\t\tAND t.ID>@PageLowerBound  ");
            builder.Append("\t\t\tAND t.ID<=@PageUpperBound ");
            builder.Append("\t\tORDER BY ");
            builder.Append("\t\t\t[ProductId] DESC ");
            builder.Append("\tEND ");
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@pagesize", SqlDbType.Int);
            commandParameters[0].Value = pagesize;
            commandParameters[1] = new SqlParameter("@pageindex", SqlDbType.Int);
            commandParameters[1].Value = pageindex;
            commandParameters[2] = new SqlParameter("@docount", SqlDbType.Bit);
            commandParameters[2].Value = docount;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, builder.ToString(), commandParameters);
            set.Tables[0].TableName = "Office_Product";
            return set.Tables[0];
        }

        public bool Update(SqlConnection conn, OfficeProductData data)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("UPDATE [dbo].[Office_Product] ");
            builder.Append("SET ");
            builder.Append("\t[TypeId] = @TypeId, ");
            builder.Append("\t[Name] = @Name, ");
            builder.Append("\t[Description] = @Description, ");
            builder.Append("\t[Unit] = @Unit, ");
            builder.Append("\t[Price] = @Price, ");
            builder.Append("\t[Supplier] = @Supplier, ");
            builder.Append("\t[LowStock] = @LowStock, ");
            builder.Append("\t[Stock] = @Stock ");
            builder.Append("WHERE ");
            builder.Append("\t[ProductId] = @ProductId ");
            SqlParameter[] commandParameters = new SqlParameter[9];
            commandParameters[0] = new SqlParameter("@ProductId", SqlDbType.Int);
            commandParameters[0].Value = data.ProductId;
            commandParameters[1] = new SqlParameter("@TypeId", SqlDbType.Int);
            commandParameters[1].Value = data.TypeId;
            commandParameters[2] = new SqlParameter("@Name", SqlDbType.NVarChar, 200);
            commandParameters[2].Value = data.Name;
            commandParameters[3] = new SqlParameter("@Description", SqlDbType.NText);
            if (data.DescriptionIsNull)
            {
                commandParameters[3].Value = DBNull.Value;
            }
            else
            {
                commandParameters[3].Value = data.Description;
            }
            commandParameters[4] = new SqlParameter("@Unit", SqlDbType.NVarChar, 20);
            commandParameters[4].Value = data.Unit;
            commandParameters[5] = new SqlParameter("@Price", SqlDbType.Decimal);
            commandParameters[5].Precision = 10;
            commandParameters[5].Scale = 2;
            if (data.PriceIsNull)
            {
                commandParameters[5].Value = DBNull.Value;
            }
            else
            {
                commandParameters[5].Value = data.Price;
            }
            commandParameters[6] = new SqlParameter("@Supplier", SqlDbType.NVarChar, 50);
            if (data.SupplierIsNull)
            {
                commandParameters[6].Value = DBNull.Value;
            }
            else
            {
                commandParameters[6].Value = data.Supplier;
            }
            commandParameters[7] = new SqlParameter("@LowStock", SqlDbType.Int);
            commandParameters[7].Value = data.LowStock;
            commandParameters[8] = new SqlParameter("@Stock", SqlDbType.Int);
            commandParameters[8].Value = data.Stock;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, builder.ToString(), commandParameters);
            return true;
        }
    }
}

